<template>
	<view>
		<view class="content_box">
			<view class="content">
				<view class="content_input2">
					<view class="view1">
						头像
					</view>
					<view class="ci_image" style="margin-right: -16rpx;">
						<u-upload width="44" height="44" :fileList="fileList1" @afterRead="afterRead" name="1" multiple :maxCount="1" :deletable="false"> 
							<image :src="imageUrl+userinfo.avatar" mode="aspectFill" v-if="userinfo.avatar">
							</image>
							<image src="@/static/images/avater.png" mode="aspectFill" v-else>
							</image>
						</u-upload>
					</view>
					<view class="">
						<u-icon name="arrow-right" color="#303030" size="14"></u-icon>
					</view>
				</view>
				<height :hg="80"></height>
				<view class="content_input">
					<view class="view1">
						昵称
					</view>
					<view style="margin-right: -16rpx;">
						<input style="color: #000000;" type="text" v-model="userinfo.nickName" placeholder="请填写您的昵称"  @blur="checkAddress" maxlength="8" disabled>
					</view>
					<view class="">
						<u-icon name="arrow-right" color="#303030" size="14"></u-icon>
					</view>
				</view>
				<view class="content_input">
					<view class="view1">
						账号/手机号
					</view>
					<view>
						<input style="color: #000000;" type="text" disabled v-model="userinfo.phonenumber"
							placeholder="请填写手机号">
					</view>
				</view>
				<view class="content_input">
					<view class="view1">
						职位
					</view>
					<view>
						<input style="color: #000000;" type="text" disabled v-model="shenfen"
							placeholder="请填写职位">
					</view>
				</view>
				<view class="content_input">
					<view class="view1">
						个性签名
					</view>
					<view>
						<input style="color: #000000;" type="text" v-model="userinfo.signature"
							placeholder="请填写个性签名">
					</view>
				</view>
			</view>
			<button @click="modify" class="ubtton">提交修改</button>
		</view>
	</view>
</template>

<script>
	import config from '@/common/config.js'
	export default {
		data() {
			return {
				fileList1: [],
				typeName: '',
				shenfen:'',
				userinfo:{},
				imageUrl:config.configURLs
			};
		},

		onShow() {
			//获取用户信息
			this.getuserInfos();
		},
		methods: {
			//监听备注长度
			checkAddress(e) {
				const inputNum = e.detail.cursor;
				this.userinfo.nickName = e.detail.value
				if(inputNum > 8){
					this.userinfo.nickName = this.userinfo.nickName.substring(0, 8);
					uni.showToast({
					  title: '输入内容过长',
					  icon: 'none'
					});
				}
			},
			//获取用户信息
			async getuserInfos(){
				var that = this
				let data = await that.$api.getprofile();
				if (data.code == 200) {
					that.shenfen = data.postGroup
					that.userinfo = data.data
				} else {
					uni.showToast({
						title: data.msg,
						icon: 'none'
					})
				}
			},
			//提交修改
			async modify(){
				let {
					nickName,
					signature
				} = this.userinfo
				let data = await this.$api.getprasdf({
					signature:signature
				});
				if(data.code == 200){
					uni.showToast({
						title: '修改成功',
						icon: 'none'
					})
					setTimeout(function() {
						uni.navigateBack();
					}, 1000);
				}else{
					uni.showToast({
						title: data.msg,
						icon: 'none'
					})
				}
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',	
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: config.configURL + 'system/user/profile/avatar', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'avatarfile',
						header: {
							Authorization:  'Bearer ' + uni.getStorageSync('token')
						},
						success: (res) => {
							let res2 = JSON.parse(res.data)
							this.userinfo.avatar = res2.imgUrl
							setTimeout(() => {
								resolve(res.data.url)
								uni.showToast({
									title: '修改成功',
									icon: 'none'
								})
							}, 1000)
						}
					});
				})
			},
		}
	}
</script>

<style lang="scss">
	::v-deep .u-upload__wrap {
		justify-content: flex-end !important;
	}

	::v-deep .u-upload__wrap__preview {
		margin: 0 auto !important;
	}

	.ubtton {
		width: 95%;
		line-height: 90rpx;
		text-align: center;
		font-size: 30rpx;
		color: white;
		background: #0CCEA1;
		margin: 40rpx auto;
		border: none;
		border-radius: 200rpx;
	}

	.content_box {
		background: #FFFFFF;
		border-radius: 16rpx;
		margin: 0 auto;

		.content {
			width: 100%;
			margin: 0 auto;
			position: relative;

			.content_img {
				position: absolute;
				left: 50%;
				top: -80rpx;
				transform: translateX(-50%);
				width: 138rpx;
				height: 138rpx;

				image {
					border-radius: 50%;
				}
			}

			.content_input {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 30rpx 24rpx;
				box-sizing: border-box;
				border-bottom: 1rpx solid #eaeaea42;

				.view1 {
					width: 30%;
					font-size: 28rpx;
					font-weight: 400;
					color: #333333;
				}

				view:nth-child(2) {
					width: 60%;
					display: flex;
					justify-content: flex-end;

					input {

						display: inline-block;
						font-size: 28rpx;
						font-weight: 400;
						color: #979797;
						text-align: right;
					}

					image {
						width: 88rpx;
						height: 88rpx;
					}
				}
			}

			.content_input2 {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 30rpx 24rpx;
				box-sizing: border-box;
				border-bottom: 1rpx solid #eaeaea42;

				.view1 {
					width: 30%;
					font-size: 28rpx;
					font-weight: 400;
					color: #333333;
				}

				.ci_image {
					width: 88rpx;
					display: flex;
					flex-direction: row;
					justify-content: flex-end;
					margin-left: 50%;

					image {
						display: inline-block;
						width: 88rpx;
						height: 88rpx;
						border-radius: 50%;
					}
				}
			}
		}
	}
</style>